import { ethers } from "ethers";
// 利用公共rpc节点连接以太坊网络
// 可以在 https://chainlist.org 上找到
const ALCHEMY_MAINNET_URL = 'https://eth-sepolia.public.blastapi.io';

// 连接以太坊主网
const provider = new ethers.JsonRpcProvider(ALCHEMY_MAINNET_URL);

// 创建一个新钱包
const wallet1 = ethers.Wallet.createRandom()
const walletWithProvider = wallet1.connect(provider);
// 获取私钥和助记词
const priveteKey = wallet1.privateKey;
const mnemonic = wallet1.mnemonic;

// 利用私钥创建钱包
const wallet2 = new ethers.Wallet('0x7ef88bf298cdee353b856773f458f6f883d86d857889dcdf663271486cfccc3a', provider)

// 利用助记词创建钱包
const wallet3 = ethers.Wallet.fromPhrase('magnet genuine custom runway vessel misery monkey kite chat diary habit decide');

const address1 = await wallet1.getAddress();
const address2 = await wallet2.getAddress();
const address3 = await wallet3.getAddress();
console.log(address1)
console.log(address2)
console.log(address3)
console.log(wallet3.mnemonic.phrase)
console.log(`address2余额：${ethers.formatEther(await provider.getBalance(address2))}ETH`)
console.log(`address3余额：${ethers.formatEther(await provider.getBalance(address3))}ETH`)

// 构造交易参数
const tx = {
    to: address3,
    value: ethers.parseEther('0.01') 
}
// 发送交易
const receipt = await wallet2.sendTransaction(tx)
// 等待链上确认交易
await receipt.wait()
console.log(receipt)

console.log(`address2余额：${ethers.formatEther(await provider.getBalance(address2))}ETH`)
console.log(`address3余额：${ethers.formatEther(await provider.getBalance(address3))}ETH`)
